package com.wnxy.producer.f_confirm;

import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.math.BigDecimal;
import java.util.Date;

@Component
public class ConfirmProducer {
    @Autowired
    private RabbitTemplate rabbitTemplate;
    @Autowired
    private RabbitConfirm rabbitConfirm;

    // 创建对象之后执行
    @PostConstruct
    public void init(){
        // 消息到达交换机的确认
        rabbitTemplate.setConfirmCallback(rabbitConfirm);
        // 消息是否成功路由到队列的确认
        rabbitTemplate.setMandatory(true);
        rabbitTemplate.setReturnCallback(rabbitConfirm);
    }

    public void producerConfirm() {
        // 对象封装消息内容
        OrderDto orderDto = OrderDto.builder()
                .oderSn("o100")
                .money(new BigDecimal("100.99"))
                .orderTime(new Date()).build();

        rabbitTemplate.convertAndSend("exchange-confirm","confirm.msg",orderDto);
    }
}